package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.acticle.domain.Destination;
import cn.wolfcode.luowowo.acticle.domain.QueryContent;
import cn.wolfcode.luowowo.acticle.domain.Region;
import cn.wolfcode.luowowo.acticle.query.DestinationQuery;
import cn.wolfcode.luowowo.acticle.query.DestinationThemeQuery;
import cn.wolfcode.luowowo.acticle.service.IDestinationService;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.RegionMapper;
import cn.wolfcode.luowowo.common.exception.LogicException;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationMapper destinationMapper;
    @Autowired
    private RegionMapper regionMapper;

    @Override
    public List<Destination> getDestByRegionId(Long rid) {
        Region region = regionMapper.selectByPrimaryKey(rid);
        Long[] refIds = region.getRefIds();
        return destinationMapper.selectDestByIds(refIds);
    }

    @Override
    public PageInfo queryForList(DestinationQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(destinationMapper.selectForList(qo));
    }

    @Override
    public List<Destination> getToasts(Long parentId) {
        List<Destination> list = new ArrayList<>();

        createToasts(parentId,list);

        return list;
    }

    @Override
    public List<Destination> queryHotDestByRegionId(Long regionId) {
        if(regionId == -1){
            return destinationMapper.selectDestByParentId(1L);
        }


        return getDestByRegionId(regionId);
    }

    @Override
    public List<Destination> getDestByDeep(int deep) {
        return destinationMapper.listByDeep(deep);
    }

    @Override
    public void changeHotValue(Long id, Long hot) throws LogicException {
        destinationMapper.updateHotValueById(id,hot);
    }

    @Override
    public void changeHotValueRegion(Long id, Long hot) throws LogicException{
        regionMapper.updateHotValueById(id,hot);
    }

    @Override
    public void setInfo(Long id, String info) throws LogicException {
        regionMapper.updateInfoById(id,info);
    }

    @Override
    public void delete(Long id) throws LogicException{
        destinationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Destination> list() {
        return destinationMapper.selectAll();
    }

    @Override
    public Destination getCountry(Long id) {
        List<Destination> toasts = this.getToasts(id);

        return toasts.size() == 0 ? null : toasts.get(0);
    }

    @Override
    public Destination getProvince(Long id) {
        List<Destination> toasts = this.getToasts(id);
        if(toasts.size() >= 2 && toasts.get(0).getId() == 1){
            return toasts.get(1);
        }
        return null;
    }

    @Override
    public PageInfo queryDestByQo(DestinationThemeQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Destination> list = destinationMapper.selectDestByQo(qo);
        return new PageInfo(list);
    }

    public Destination queryDestByUserCity(String city) {
        Destination destination = destinationMapper.selectDestByUserCity(city);
        return destination;
    }

    @Override
    public List<Destination> queryDestinationByMonth(Integer viewMonth) {
        return destinationMapper.selectDestinationByMonth(viewMonth);
    }

    private void createToasts(Long parentId, List<Destination> list) {
        //获取当前dest
        Destination dest = destinationMapper.selectByPrimaryKey(parentId);
        //如果当前dest不存在则返回
        if(dest == null){
            return;
        }
        //如果不为空
        list.add(0,dest);
        //如果当前的dest的parentId不为空  则迭代
        if(dest.getParent() != null){
            createToasts(dest.getParent().getId(),list);
        }
    }
}
